package com.hp.bon.sgw.stdbss30.bizSlotOutside;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class QueryServBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(QueryServBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();

        try {
            String areaCode = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Area_Code");
            String value = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Value");


            //构建请求条件
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("accNum", areaCode+value);
            jsonObject.put("objAttr", "99");
            StringBuilder msgBodyStr = new StringBuilder();

            //发送请求
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            msgBodyStr.append(jsonObject.toJSONString());
               _log.info("request body is {}",msgBodyStr.toString());_log.info("request body is {}",msgBodyStr.toString());
String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), webserviceUrl);
_log.info("response is "+respStr.toString());

            if (respStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }

            //解析json 拼接报文
            JSONObject json = JSON.parseObject(respStr);
            StringBuffer buffer = new StringBuffer();
            jsonToXmlStr(xmlMessage, json, buffer);


            bizResult.setHttpBody(buffer.toString());
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(XmlMessage xmlMessage, JSONObject json, StringBuffer buffer) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        buffer.append("<SOAP-ENV:Header/>");
        buffer.append("<SOAP-ENV:Body>");
        buffer.append("<ns3:SgwQueryAck xmlns:ns3=\"http://www.chinatelecom.cn/SGW/SOAP\">");
        buffer.append("<MID>" + xmlMessage.getMID() + "</MID>");
        buffer.append("<REPTAG>" + xmlMessage.getREPTAG() + "</REPTAG>");
        buffer.append("<RTT>" + xmlMessage.getRTT() + "</RTT>");
        buffer.append("<SIG>" + xmlMessage.getSIG() + "</SIG>");
        buffer.append("<RC>" + xmlMessage.getRC() + "</RC>");
        buffer.append("<DS>" + xmlMessage.getDS() + "</DS>");
        buffer.append("<DATA><![CDATA[<Service_Information>");

        buffer.append("<Result>" + (json.get("resultCode") == null ? "" : json.get("resultCode")) + "</Result>");
        buffer.append("<Data_Information>");
        buffer.append("<CustName></CustName>");
        buffer.append("<serv_id>" + (json.get("prodInstId") == null ? "" : json.get("prodInstId")) + "</serv_id>");
        buffer.append("<ProductId>0</ProductId>");
        buffer.append("<ProductName></ProductName>");
        buffer.append("<HomeAreaCode>" + (json.get("regionId") == null ? "" : json.get("regionId")) + "</HomeAreaCode>");

        if (json.get("statusCd").equals("F1R")) {
            buffer.append("<ServState>3</ServState>");
        } else if (json.get("statusCd").equals("F1S")) {
            buffer.append("<ServState>1</ServState>");
        }else{
            buffer.append("<ServState>0</ServState>");
        }

        buffer.append("<StateDate>" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "</StateDate>");
        buffer.append("<PaymentFlag>" + (json.get("paymentFlag") == null ? "" : json.get("paymentFlag"))  + "</PaymentFlag>");
        buffer.append("<GroupUser>1</GroupUser>");
        buffer.append("</Data_Information>");

        buffer.append("</Service_Information>]]></DATA>");
        buffer.append("</ns3:SgwQueryAck>");
        buffer.append("</SOAP-ENV:Body>");
        buffer.append("</SOAP-ENV:Envelope>");
    }

}
